package com.tianqu.tms.www.car.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dto.CodesDto;
import com.tianqu.tms.entity.Codes;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TmsCodeAjaxAction extends TmsAjaxAction {

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {
		String itemId = (String) uc.getParameter("itemId");
		String query = request.getParameter("query");

		List r = new ArrayList();
		Codes code = Codes.getInstance();
		List<CodesDto> list = code.getCodes(Integer.parseInt(itemId), query);
		for (CodesDto info : list) {
			Map map = null;
			map = new HashMap();
			map.put("value", info.getItemValue());
			map.put("data", info.getItemCode());
			r.add(map);
		}

		JSONArray jsonArray = JSONArray.fromObject(r);
		JSONObject json = new JSONObject();
		json.put("query", query);
		json.put("suggestions", jsonArray);
		responseValues(response, json.toString(), TEXT);
	}
}
